const apollo = require('ctrip-apollo')

module.exports = class APOLLO_NAMESPACE {
    constructor(host, appId) {
      const appApollo = apollo({
        host,
        appId
      })
      this.namespace = appApollo.namespace()
    }
    bindChange() {
      this.namespace.on('change', ({
        key,
        oldValue,
        newValue
      }) => {
        // Updates new values to `process.env`
        process.env[key] = newValue
      })
    }
    onReady() {
      return this.namespace.ready().then(() => {
        Object.entries(this.namespace.config()).forEach(item => {
          console.log('配置：', item)
          process.env[item[0]] = item[1]
        })
      })
    }
  }